package com.mmlc.meimei.lichengkongjian.home;

import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.requestbean.BaseResponseBean;
import com.androidnetframe.responsejavabean.RequestError;
import com.androidnetframe.utils.LogUtils;
import com.easemob.helpdeskdemo.Constant;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.GoodsCollectRequestFilter;
import com.mmlc.network.filter.GoodsDetailRequestFilter;
import com.mmlc.network.responsebean.GoodsDetailResponseBean;
import com.mmlc.utils.ToastUtils;
import com.mmlc.utils.ViewUtils;
import com.mmlc.widget.ViewInfoBannerPager;

public class NewProductInfoActivity extends InterActivity {

    @Override
    protected int initContentView() {
        return R.layout.activity_new_product_info;
    }

    String goods_id;
    DisplayMetrics metrics;

    @Override
    protected void initExtra() {
        super.initExtra();
        goods_id = getIntent().getStringExtra("goods_id");
        metrics = getResources().getDisplayMetrics();
    }

    ScrollView scv_home;
    ViewInfoBannerPager info_banner;
    ImageView iv_like;
    TextView tv_name;
    WebView web_html;

    @Override
    protected void initView() {
        scv_home = ViewUtils.getView(activity, R.id.scv_home);
        iv_like = ViewUtils.getView(activity, R.id.iv_like);

        info_banner = ViewUtils.getView(this, R.id.info_banner);

        tv_name = ViewUtils.getView(activity, R.id.tv_name);
        web_html = ViewUtils.getView(activity, R.id.web_html);
    }

    @Override
    protected void onResume() {
        super.onResume();
        scv_home.smoothScrollTo(0, 0);
    }

    GoodsDetailResponseBean goodsDetail;

    @Override
    protected void initInfo() {
        super.initInfo();

        GoodsDetailRequestFilter filter = new GoodsDetailRequestFilter(activity);
        filter.requestBean.goods_id = goods_id;
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<GoodsDetailResponseBean>() {
            @Override
            public void onSuccess(GoodsDetailResponseBean result) {
                goodsDetail = result;
                info_banner.setDatas(result.getData().getGoods_album());
                displayGoodsDetail(result.getData());
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                LogUtils.e(TAG, "failedType:" + failedType + "/ serverError:" + serverError.toString());
            }
        });
    }

    private void displayGoodsDetail(GoodsDetailResponseBean.DataBean data) {
        if (data.getIs_collect() == 1) {
            iv_like.setImageResource(R.drawable.liked);
        } else iv_like.setImageResource(R.drawable.like);

        if (!ViewUtils.isEmpty(data.getGoods_name()))
            tv_name.setText(data.getGoods_name());
        if (!ViewUtils.isEmpty(data.getContent())) {
            web_html.getSettings().setJavaScriptEnabled(true);
            web_html.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            web_html.setWebChromeClient(new WebChromeClient());
            web_html.getSettings().setSupportZoom(true);
            web_html.getSettings().setDisplayZoomControls(false);
            web_html.getSettings().setBuiltInZoomControls(false);
            web_html.getSettings().setUseWideViewPort(true);
            web_html.getSettings().setLoadWithOverviewMode(true);
            web_html.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
            web_html.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
            web_html.requestFocus();
            web_html.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

            web_html.loadDataWithBaseURL(null, data.getContent(), "text/html", "utf-8", null);
            scv_home.scrollTo(0, 0);
        }
    }

    @Override
    protected void initEvent() {
        ViewUtils.getView(activity, R.id.iv_back).setOnClickListener(this);
        ViewUtils.getView(activity, R.id.iv_like).setOnClickListener(this);
        ViewUtils.getView(activity, R.id.iv_service).setOnClickListener(this);

        ViewUtils.getView(activity, R.id.tv_pre_order).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_like:
                /** collection */
                goodsCollect();
                break;
            case R.id.iv_service:
                /** start conversation to service */
                startActivity(new Intent(this, com.easemob.helpdeskdemo.ui.LoginActivity.class).putExtra(Constant.MESSAGE_TO_INTENT_EXTRA,
                        Constant.MESSAGE_TO_PRE_SALES));
                break;
            case R.id.tv_pre_order:
                /** buy popupwindow */

                break;
        }
    }

    private void goodsCollect() {
        GoodsCollectRequestFilter filter = new GoodsCollectRequestFilter(activity);
        filter.requestBean.method = goodsDetail.getData().getIs_collect() == 0 ? "goodsCollect" : "goodsUnCollect";
        filter.requestBean.goods_id = goodsDetail.getData().getId();
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<BaseResponseBean>() {
            @Override
            public void onSuccess(BaseResponseBean result) {
                goodsDetail.getData().setIs_collect(0 == goodsDetail.getData().getIs_collect() ? 1 : 0);
                if (goodsDetail.getData().getIs_collect() == 1)
                    iv_like.setImageResource(R.drawable.liked);
                else iv_like.setImageResource(R.drawable.like);
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast("操作失败");
            }
        });
    }
}
